<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\stat;

use app\enum\QqEnum;
use app\model\QqAdvertiserDailyStatModel;
use app\service\BaseService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

class QqAdvertiserStatService extends BaseService implements StatModelInterface
{
    /**
     * 获取广告账户流水信息.
     * @param string $advertiserId 账户ID
     * @param int $statDate 日期
     * @param array $params 表参数
     * @return null|array|mixed|Model|QqAdvertiserDailyStatModel
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getStatInfo(string $advertiserId = '', int $statDate = 0, array $params = [])
    {
        $statDate = date('Ymd', $params['time']);
        $where = [
            ['advertiser_id', '=', $advertiserId],
            ['stat_date', '=', $statDate],
            ['fund_type', '=', $params['fund_type'] ?? ''],
        ];
        return (new QqAdvertiserDailyStatModel())->where($where)->find();
    }

    /**
     * 修改报表信息.
     * @param Model $model 表模型
     * @param array $params 表参数
     */
    public static function updateStatData(Model $model, array $params = []): bool
    {
        if (! $model instanceof QqAdvertiserDailyStatModel) {
            return self::setError('报表模型错误');
        }
        try {
            $params = self::filterParams($params);
            $model->setAttrs($params);
            return $model->save();
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 插入报表信息.
     * @param string $advertiserId 账户id
     * @param array $params 表参数
     */
    public static function insertStatData(string $advertiserId = '', array $params = []): bool
    {
        try {
            $params['stat_date'] = date('Ymd', $params['time']);
            $params = self::filterParams($params);
            $accountInfo = BaseService::getClientAccountInfo($advertiserId);
            $params['advertiser_id'] = $advertiserId;
            $params['project_id'] = $accountInfo['project_id'];
            $params['staff_id'] = $accountInfo['staff_id'];
            $model = new QqAdvertiserDailyStatModel();
            $model->setAttrs($params);
            return $model->save();
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 对比数据是否相同.
     * @param Model $model 表模型
     * @param array $param 表参数
     */
    public static function isSame(Model $model, array $param = []): bool
    {
        $difField = [
            'deposit', 'paid', 'trans_in', 'trans_out', 'credit_modify', 'balance', 'preauth_balance',
        ];
        // 验证数据是否相同
        foreach ($difField as $val) {
            if ($model->getAttr($val) != $param[$val]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 过滤消耗数据.
     * @param array $data 接口数据
     */
    private static function filterParams(array $data = []): array
    {
        if ($data) {
            $paid = $data['paid'] / 100;
            $data['cost'] = 0;
            $data['cash_cost'] = 0;
            $data['reward_cost'] = 0;
            if ($data['fund_type'] == QqEnum::FUND_TYPE_VIRTUAL || $data['fund_type'] == QqEnum::FUND_TYPE_SPECIAL_GIFT || $data['fund_type'] == QqEnum::FUND_TYPE_FLOW_SOURCE_AD_FUND) {
                $data['cost'] += $paid;
                if ($data['fund_type'] == QqEnum::FUND_TYPE_FLOW_SOURCE_AD_FUND) {
                    $data['reward_cost'] += $paid;
                }
            } else {
                $data['cost'] += $paid;
                $data['cash_cost'] += $paid;
            }
            //            if ($data['fund_type'] == QqEnum::FUND_TYPE_CASH) {// 现金消耗
            //                $data['cost'] += $paid;
            //                $data['cash_cost'] += $paid;
            //            } elseif ($data['fund_type'] == QqEnum::FUND_TYPE_VIRTUAL) {// 补偿虚拟金消耗
            //                $data['cost'] += $paid;
            //            } elseif ($data['fund_type'] == QqEnum::FUND_TYPE_ROLL) {// 固定信用金消耗
            //                $data['cost'] += $paid;
            //                $data['cash_cost'] += $paid;
            //            }
        }
        return $data;
    }
}
